package com.kz.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.kz.model.Camera;



public class GetContentService {
   
    HttpResponse httpresponse = null;
    InputStream is = null;
    HttpEntity httpentity =null;
    List<Camera>  lightslist = new ArrayList<Camera>();  
    List resultList;
    
    public List getContent(String weburl){
    	
         weburl = "http://www.toronto.ca/transportation/redlight_cameras/locations.htm";
      
        HttpGet httpget = new HttpGet(weburl);
    	HttpClient httpclient = new DefaultHttpClient();
        //HttpHost proxy = new HttpHost("172.29.141.42", 8080);
        // httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        
        
        try {
			httpresponse = httpclient.execute(httpget);
		    httpentity = 	httpresponse.getEntity();
		    is = httpentity.getContent();
		    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		    String html ="";
			String line ="";
			while((line = reader.readLine())!=null){
			//	if(line)
			html += line;	
			}
			resultList = getContextByTag(html,"td");
			
			List<String> addr1 = new ArrayList<String>();
			List<String> addr2 = new ArrayList<String>();
			
			int index = 1;
			
	        for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
	            String context = (String) iterator.next();
	           
	            context = this.elementNotag(context);
	            
	            System.out.println(context);
	            if(index%2!=0){
	            	addr1.add(context);
	            }
	            else{
	            	addr2.add(context);
	            }
	             index++;
	        }
	        System.out.println(index+"=======================================");
	        for(int i=0;i<addr1.size();i++){
	        	Camera l = new Camera(addr1.get(i),addr2.get(i));
	        	lightslist.add(l);
	        }
	        
	        
		    return lightslist;
			
		} catch (ClientProtocolException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
    	
    	
		return lightslist;
    	//String response = httpclient.execute(httpget,responseHandler);
//    	HttpEntity entity = response.getEntity();
//    	if (entity != null) {
//    	InputStream instream = entity.getContent();
//    	int l;
//    	byte[] tmp = new byte[2048];
//    	while ((l = instream.read(tmp)) != -1) {
//    	}
//    	}
    	
    	 //response = "<input type="hidden" name="%%ModDate" value="00000EA8017CABD7"><div id=wrapper>";
    }
    
    public static List getContextByTag(String html,String tag) {
    	String reg="<"+tag+">.*?</"+tag+">";
        List resultList = new ArrayList();
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(html );
        while (m.find()) {
            resultList.add(m.group());
        }
        return resultList;
    }
    
    public static String elementNotag(String s)
    {
    	return s.replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "").replaceAll("</[a-zA-Z]+[1-9]?>", "");
    }
    	
    	
    }
    

